#!/usr/bin/env bash

source "$rvm_scripts_path/functions/pkg"

requirements_rvm_pkg_wait_key()
{
  rvm_is_a_shell_function no_warning ||
  {
    rvm_error "
You are using 'rvm autolibs rvm_pkg', this does not know how to install some of the packages, make sure to install those first.
"
    return 1
  }
  rvm_log "press any key to continue"
  \typeset _read_char_flag anykey
  [[ -n "${ZSH_VERSION:-}" ]] && _read_char_flag=k || _read_char_flag=n
  builtin read -${_read_char_flag} 1 -s -r anykey
}

requirements_rvm_pkg_install_custom()
{
  if
    (( $# > 0 ))
  then
    rvm_warn "Before continuing install: $*"
    requirements_rvm_pkg_wait_key || return $?
  fi
}

requirements_rvm_pkg_before()
{
  __rvm_which pkg-config >/dev/null 2>&1 ||
  {
    rvm_error "
ERROR: can not find 'pkg-config', it is required for autolibs rvm_pkg to work, install it before continuing.
"
    return 1
  }
  rvm_warn "
Warning, you are using 'rvm autolibs rvm_pkg', this is rarely used and can produce errors,
make sure to report any problems to https://github.com/rvm/rvm/issues
"
}

requirements_rvm_pkg_update_system()
{
  rvm_warn "Always update your system first!"
}

requirements_rvm_pkg_define()
{
  case "$1" in
    (rvm)
      requirements_check_custom bash curl patch
      ;;

    (jruby*)
      requirements_check make

      if
        is_head_or_disable_binary "$1"
      then
        requirements_check_custom git
        case $( jruby_installation_method  "$1" ) in
          ant) requirements_check_custom ant             ;;
          mvn) requirements_check_custom_after mvn=maven ;;
        esac
      fi

      requirements_check_custom java
      ;;

    (ir*)
      requirements_check_custom mono
      ;;

    (opal)
      requirements_check_custom node
      ;;

    (yaml|zlib|openssl|readline|xml2|xslt)
      true # just skip the packages
      ;;

    (*-head)
      requirements_check_custom git
      requirements_rvm_pkg_define "${1%-head}"
      ;;

    (*)
      requirements_check_custom gcc g++ libtool bison bzip2
      requirements_check yaml zlib openssl readline
      ;;
  esac
}

requirements_rvm_pkg_after()
{
  requirements_rvm_pkg_configure "$1" yaml zlib openssl readline
}
